import Vue from 'vue'
import VueRouter from 'vue-router'
import Footer from '@/components/Footer.vue'
// import store from '@/store'

Vue.use(VueRouter)

const routes = [

  { path: '/', redirect: '/home' },
  // { path: '/home', name: 'home', component: () => import(/* webpackChunkName: "base" */ '../views/home/index.vue') },
  // { path: '/cart', name: 'cart', component: () => import(/* webpackChunkName: "base" */ '../views/cart/index.vue') },
  // { path: '/kind', name: 'kind', component: () => import(/* webpackChunkName: "base" */ '../views/kind/index.vue') },
  // { path: '/user', name: 'user', component: () => import(/* webpackChunkName: "base" */ '../views/user/index.vue') },
  {
    path: '/home',
    name: 'home',
    components: {
      default: () => import(/* webpackChunkName: "base" */ '../views/home/index.vue'),
      footer: Footer
    },
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/cart',
    name: 'cart',
    components: {
      default: () => import(/* webpackChunkName: "base" */ '../views/cart/index.vue'),
      footer: Footer
    }//,
    // 路由独享的守卫
    // beforeEnter: (to, from, next) => {
    //   if (localStorage.getItem('loginState') === 'true') {
    // 或if (store.state.user.loginState)
    //     next()
    //   } else {
    //     next('/login')
    //   }
    // }
  },
  {
    path: '/kind',
    name: 'kind',
    components: {
      default: () => import(/* webpackChunkName: "base" */ '../views/kind/index.vue'),
      footer: Footer
    },
    meta: {
      keepAlive: true
    }
  },
  {
    path: '/user',
    name: 'user',
    components: {
      default: () => import(/* webpackChunkName: "base" */ '../views/user/index.vue'),
      footer: Footer
    }
  },
  { path: '/register', name: 'register', component: () => import(/* webpackChunkName: "user" */ '../views/register/index.vue') },
  { path: '/login', name: 'login', component: () => import(/* webpackChunkName: "user" */ '../views/login/index.vue') },
  {
    path: '/detail/:proid',
    name: 'detail',
    component: () => import(/* webpackChunkName: "user" */ '../views/detail/index.vue')
    // TODO
    //, 路由组件传参
    // props: {
    //   default: true,
    //   footer: true
    // }
  },
  {
    // 确认订单，依靠的时间 + userid
    path: '/order/:time', name: 'order', component: () => import(/* webpackChunkName: "order" */ '../views/order/index.vue')
  },
  {
    // 地址选择路由
    path: '/orderaddresslist/:time', name: 'orderaddresslist', component: () => import(/* webpackChunkName: "order" */ '../views/order/list.vue')
  },
  {
    // 添加地址路由
    path: '/orderaddressadd/:time', name: 'orderaddressadd', component: () => import(/* webpackChunkName: "order" */ '../views/order/add.vue')
  },
  {
    // 地址选择路由
    path: '/orderaddressedit/:time/:addressid', name: 'orderaddressedit', component: () => import(/* webpackChunkName: "order" */ '../views/order/edit.vue')
  },
  {
    // 订单支付路由
    path: '/pay/:time', name: 'pay', component: () => import(/* webpackChunkName: "order" */ '../views/pay/index.vue')
  },
  {
    // 搜索列表
    path: '/searchlist', name: 'searchlist', component: () => import(/* webpackChunkName: "order" */ '../views/search/list.vue')
  },
  {
    // 搜索页面
    path: '/search', name: 'search', component: () => import(/* webpackChunkName: "order" */ '../views/search/index.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior (to, from, savedPosition) {
    console.log('savedPosition', savedPosition)
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

// 全局的导航守卫
// router.beforeEach((to, from, next) => {
//   console.log(to)
//   if (to.name === 'cart') {
//     if (localStorage.getItem('loginState') !== 'true') {
//       next('/login')
//     } else {
//       next()
//     }
//   } else {
//     next()
//   }
// })

export default router
